<template>
	<view class="mine-container" :style="{height: `${windowHeight}px`}">
		<!--顶部个人信息栏-->
		<view class="header-section">
			<view class="flex padding justify-between">
				<view class="flex align-center">
					<view v-if="!avatar" class="cu-avatar xl round bg-white">
						<view class="iconfont icon-people text-gray icon"></view>
					</view>
					<image v-if="avatar" @click="handleToAvatar" :src="avatar" class="cu-avatar xl round"
						mode="widthFix">
					</image>
					<view v-if="!name" @click="handleToLogin" class="login-tip">
						点击登录
					</view>
					<view v-if="name" @click="handleToInfo" class="user-info">
						<view class="u_title">
							用户名：{{ name }}
						</view>
					</view>
				</view>
				<view @click="handleToInfo" class="flex align-center">
					<text>个人信息</text>
					<view class="iconfont icon-right"></view>
				</view>
			</view>
		</view>

		<view class="content-section">
			<view class="mine-actions grid col-4 text-center">
				<!--        <view class="action-item" @click="handleJiaoLiuQun">
          <view class="iconfont icon-friendfill text-pink icon"></view>
          <text class="text">交流群</text>
        </view> -->
				<view class="action-item" @click="handleToCustomer">
					<view class="iconfont icon-service text-blue icon"></view>
					<text class="text">在线客服</text>
				</view>
				<view class="action-item" @click="handleBuilding">
					<view class="iconfont icon-community text-mauve icon"></view>
					<text class="text">反馈社区</text>
				</view>
				<view class="action-item" @click="handleBuilding">
					<view class="iconfont icon-dianzan text-green icon"></view>
					<text class="text">点赞我们</text>
				</view>
			</view>

			<view class="menu-list">
				<view class="list-cell list-cell-arrow" @click="handleToEditInfo">
					<view class="menu-item-box">
						<view class="iconfont icon-user menu-icon"></view>
						<view>编辑资料</view>
					</view>
				</view>
				<view class="list-cell list-cell-arrow" @click="handleHelp">
					<view class="menu-item-box">
						<view class="iconfont icon-help menu-icon"></view>
						<view>常见问题</view>
					</view>
				</view>
				<view class="list-cell list-cell-arrow" @click="handleAbout">
					<view class="menu-item-box">
						<view class="iconfont icon-aixin menu-icon"></view>
						<view>关于我们</view>
					</view>
				</view>
				<view class="list-cell list-cell-arrow" @click="handleToSetting">
					<view class="menu-item-box">
						<view class="iconfont icon-setting menu-icon"></view>
						<view>应用设置</view>
					</view>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import storage from '@/utils/storage'
	import config from '@/config'

	export default {
		data() {
			return {
				name: this.$store.state.user.name,
				version: getApp().globalData.config.appInfo.version,
				loading: false
			}
		},
		computed: {
			avatar() {
				return this.$store.state.user.avatar
			},
			windowHeight() {
				return uni.getSystemInfoSync().windowHeight - 50
			}
		},
		methods: {
			handleToInfo() {
				this.$tab.navigateTo('/pages/mine/info/index')
			},
			handleToEditInfo() {
				this.$tab.navigateTo('/pages/mine/info/edit')
			},
			handleToSetting() {
				this.$tab.navigateTo('/pages/mine/setting/index')
			},
			handleToLogin() {
				this.$tab.reLaunch('/pages/login')
			},
			handleToAvatar() {
				this.$tab.navigateTo('/pages/mine/avatar/index')
			},
			handleLogout() {
				this.$modal.confirm('确定注销并退出系统吗？').then(() => {
					this.$store.dispatch('LogOut').then(() => {
						this.$tab.reLaunch('/pages/index')
					})
				})
			},
			handleHelp() {
				this.$tab.navigateTo('/pages/mine/help/index')
			},
			handleAbout() {
				this.$tab.navigateTo('/pages/mine/about/index')
			},
			handleJiaoLiuQun() {
				this.$modal.showToast('微信搜索 naidaguo 后，添加好友后拉你进技术交流群')
			},
			handleBuilding() {
				this.$modal.showToast('模块建设中~')
			},
			async handleToCustomer() {
				// 防止重复点击
				if (this.loading) return

				this.loading = true

				try {
					// 显示加载提示
					uni.showLoading({
						title: '连接客服中...',
						mask: true
					})

					// 1. 获取登录凭证
					const [err, res] = await uni.login({
					  provider: 'weixin'
					})
				
					if (err || !res.code) {
					  throw new Error("无法获取微信登录 code")
					}
				
					const code = res.code

					// 2. 通过code获取openid
					const openid = await this.getOpenIdFromServer(code)

					// 3. 初始化插件
					await this.initChatbotPlugin(openid)

					// 4. 跳转到客服页面
					uni.hideLoading()
					uni.navigateTo({
						url: '/pages/mine/customer/index'
					})

				} catch (error) {
					console.error('联系客服失败:', error)
					uni.hideLoading()
					uni.showToast({
						title: '连接客服失败，请重试',
						icon: 'none',
						duration: 3000
					})
				} finally {
					this.loading = false
				}
			},
			// 从服务器获取openid
			getOpenIdFromServer(code) {
				return new Promise((resolve, reject) => {
					uni.request({
						url: config.baseUrl + config.baseApi + '/system/auth/getOpenId',
						method: 'POST',
						data: {
							code
						},
						success: (res) => {
							if (res.data && res.data.openid) {
								resolve(res.data.openid)
							} else {
								reject(new Error('获取openid失败: ' + JSON.stringify(res.data)))
							}
						},
						fail: (err) => {
							reject(new Error('网络请求失败: ' + JSON.stringify(err)))
						}
					})
				})
			},

			// 初始化微信对话插件
			initChatbotPlugin(openid) {
				return new Promise((resolve, reject) => {
					// #ifdef MP-WEIXIN
					const plugin = requirePlugin('chatbot')
					plugin.init({
						appid: 'kZz0iP2gXknpxJ8fCRtct28FOv3eW9', // 替换为你的机器人APPID
						openid: openid,
						success: () => {
							console.log('客服插件初始化成功')
							resolve()
						},
						fail: (err) => {
							reject(new Error('插件初始化失败: ' + JSON.stringify(err)))
						}
					})
					// #endif

					// 非微信小程序环境直接成功（用于兼容）
					// #ifndef MP-WEIXIN
					resolve()
					// #endif
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f5f6f7;
	}

	.mine-container {
		width: 100%;
		height: 100%;


		.header-section {
			padding: 15px 15px 45px 15px;
			background-color: #3c96f3;
			color: white;

			.login-tip {
				font-size: 18px;
				margin-left: 10px;
			}

			.cu-avatar {
				border: 2px solid #eaeaea;

				.icon {
					font-size: 40px;
				}
			}

			.user-info {
				margin-left: 15px;

				.u_title {
					font-size: 18px;
					line-height: 30px;
				}
			}
		}

		.content-section {
			position: relative;
			top: -50px;

			.mine-actions {
				margin: 15px 15px;
				padding: 20px 0px;
				border-radius: 8px;
				background-color: white;

				.action-item {
					.icon {
						font-size: 28px;
					}

					.text {
						display: block;
						font-size: 13px;
						margin: 8px 0px;
					}
				}
			}
		}
	}
</style>